package com.hackerrank.challenges.detecthtmllinks;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Solution {
	private static class Tag {
		public static final String ANCHOR = "a", HREF = "href", TEXT = "text", VALUE = "value";
		String name;
		Map<String, String> attr = new HashMap<String, String>();
		List<Tag> body = new ArrayList<Tag>();
		public Tag(String name) {
			this.name = name;
		}
		private String getBody() {
			if(name.equals(TEXT))
				return attr.get(VALUE);
			StringBuilder ret = new StringBuilder();
			for(Tag tag : body)
				ret.append(tag.getBody());
			return ret.toString();
		}
		public String toString() {
			StringBuilder b = new StringBuilder("<");
			b.append(name);
			for(String attrName : attr.keySet()) {
				String attrValue = attr.get(attrName);
				b.append(" ").append(attrName).append("=").append(attrValue);
			}
			b.append(">");
			for(Tag tag : body)
				b.append(tag.toString());
			b.append("</").append(name).append(">");
			return b.toString();
		}
	}
	private static class TagParser {
		int i;
		char c[];
		public List<Tag> parseBody(String s) {
			i = 0;
			c = s.toCharArray();
			return parseBody();
		}
		private List<Tag> parseBody() {
			List<Tag> l = new ArrayList<Tag>();
			try {
				while(i < c.length) {
					while(Character.isSpaceChar(c[i]))
						i++;
					StringBuilder value = new StringBuilder();
					// considering end of input as end of value
					while(i < c.length && c[i] != '<')
						value.append(c[i++]);
					if(value.length() > 0) {
						Tag text = new Tag(Tag.TEXT);
						text.attr.put(Tag.VALUE, value.toString());
						l.add(text);
					}
					int j = i;
					while(Character.isSpaceChar(c[++j]));
					if(c[j] == '/')
						break;
					l.add(parseTag());
				}
			} catch(Exception e) {
				// expecting only ArrayIndexOutOfBoundsException, therefore ignoring
			}
			return l;
		}
		private Tag parseTag() {
			assert(c[i] == '<');
			while(Character.isSpaceChar(c[++i]));
			StringBuilder tagName = new StringBuilder();
			// considering end of input as end of tag name
			while(i < c.length && !Character.isSpaceChar(c[i]) && c[i] != '>')
				tagName.append(c[i++]);
			Tag tag = new Tag(tagName.toString());
			try {
				while(Character.isSpaceChar(c[i]))
					i++;
				while(c[i] != '>') {
					StringBuilder attrName = new StringBuilder(), attrValue = new StringBuilder();
					// considering end of input as end of attribute name
					while(i < c.length && !Character.isSpaceChar(c[i]) && c[i] != '=' && c[i] != '>')
						attrName.append(c[i++]);
					try {
						while(Character.isSpaceChar(c[i]))
							i++;
						if(c[i] == '=') {
							while(Character.isSpaceChar(c[++i]));
							if(c[i] == '"') {
								while(c[++i] != '"')
									attrValue.append(c[i]);
								i++;
							} else if(c[i] == '\'') {
								while(c[++i] != '\'')
									attrValue.append(c[i]);
								i++;
							} else {
								while(!Character.isSpaceChar(c[i]) && c[i] != '>')
									attrValue.append(c[i++]);
							}
							while(Character.isSpaceChar(c[i]))
								i++;
						}
					} catch(Exception e) {
						// expecting only ArrayIndexOutOfBoundsException, therefore ignoring
					}
					tag.attr.put(attrName.toString(), attrValue.toString());
				}
				i++;
				tag.body = parseBody();
				int j = i;
				assert(c[j] == '<');
				while(Character.isSpaceChar(c[++j]));
				assert(c[j] == '/');
				while(Character.isSpaceChar(c[++j]));
				StringBuilder endTagName = new StringBuilder();
				while(!Character.isSpaceChar(c[j]) && c[j] != '>')
					endTagName.append(c[j++]);
				if(!endTagName.toString().equals(tagName.toString()))
					return tag;
				while(Character.isSpaceChar(c[j]))
					j++;
				assert(c[j] == '>');
				i = j + 1;
			} catch(Exception e) {
				// expecting only ArrayIndexOutOfBoundsException, therefore ignoring
			}
			return tag;
		}
	}
	private static class Anchor {
		String href, body;
		public Anchor(String href, String body) {
			this.href = href;
			this.body = body;
		}
	}
	private static List<Anchor> getAnchors(List<Tag> tags) {
		List<Anchor> anchors = new ArrayList<Anchor>();
		for(int i = 0; i < tags.size(); i++) {
			Tag tag = tags.get(i);
			if(tag.name.equals(Tag.ANCHOR))
				anchors.add(new Anchor(tag.attr.get(Tag.HREF), tag.getBody()));
			else if(!tag.name.equals(Tag.TEXT))
				anchors.addAll(getAnchors(tag.body));
		}
		return anchors;
	}
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		in.nextLine();
		TagParser tagParser = new TagParser();
		for(int n = 0; n < N; n++) {
			List<Tag> tags = tagParser.parseBody(in.nextLine());
			List<Anchor> anchors = getAnchors(tags);
			for(Anchor anchor : anchors)
				System.out.println(anchor.href + "," + anchor.body);
		}
	}
}
